/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controllers;

import com.entities.CompanyDenomination;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author ben
 */
@Stateless
public class CompanyDenominationController {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    
    @PersistenceContext
    EntityManager em;
    
        private static final Logger logger = Logger.getLogger(CompanyDenominationController.class.getName());


    public Long save(CompanyDenomination companyDenomination)
    {
        
        try {
            em.persist(companyDenomination);
            logger.info("Saving Voucher complete");
            return companyDenomination.getId();
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Error saving Voucher");
            e.printStackTrace();
            return -1L;
        }
    }
    
    public CompanyDenomination findById(Long id)
    {
        try {
            CompanyDenomination companyDenomination = em.find(CompanyDenomination.class, id);
            logger.log(Level.INFO, "CompanyDenomination found {0}", companyDenomination.getId().toString());
            return companyDenomination;
            
        } catch (Exception e) {
                        logger.log(Level.SEVERE, "Could not find CompanyDenomination");
                        return null;

        }
           
    }
    
    public List<CompanyDenomination> getAllCompanyDenominations()
    {
        try {
             Query findCompanyQuery = em.createNamedQuery("Voucher.findAll");
            List<CompanyDenomination> companyDenominations= findCompanyQuery.getResultList();
            System.out.println("Found list of Company Denominations");
            return companyDenominations;
            
        } catch (Exception e) {
            e.printStackTrace();
            return null;
            
        }
    }
            
}
